import { FormInstance } from "antd";

export const loginRules = {
  userRules: [
    { required: true, message: "账号不能为空" },
    {
      max: 16,
      message: "账号不能超过16个字符",
    },
    {
      min: 8,
      message: "账号不能少于8位",
    },
  ],
  passwordRules: [
    { required: true, message: "密码不能为空" },
    {
      max: 16,
      message: "密码不能超过16个字符",
    },
    {
      min: 8,
      message: "密码不能少于8位",
    },
  ],
  phoneRules: [
    {
      validator: (rule: any, val: string) => {
        console.log(rule);
        const PhoneReg =
          /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/;
        switch (true) {
          case !Boolean(val):
            return Promise.reject("不能为空");
          case !PhoneReg.test(val):
            return Promise.reject("手机号码不对");
          default:
            return Promise.resolve();
        }
      },
    },
  ],
  codeRules: [
    { required: true, message: "验证码不能为空" },
    {
      max: 6,
      message: "验证码不能超过6个字符",
    },
    {
      min: 6,
      message: "验证码不能少于6位",
    },
  ],
  comfirmPasswordRules: function (form: FormInstance) {
    return [
      {
        validator: (rule: any, val: string) => {
          switch (true) {
            case !Boolean(val):
              return Promise.reject("不能为空");
            case form.getFieldValue("password") !== val:
              return Promise.reject("两次密码不一致");
            default:
              return Promise.resolve();
          }
        },
      },
    ];
  },
};
